package com.myblog.myblog.controller.admin;

import com.myblog.myblog.poge.Blog;
import com.myblog.myblog.poge.User;
import com.myblog.myblog.service.AdminBlogService;
import com.myblog.myblog.service.TagService;
import com.myblog.myblog.service.TypeService;
import com.myblog.myblog.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/admin")
public class AdminblogController {
    private static final String INPUT = "admin/admininput";
    private static final String LIST = "admin/adminblog";
    private static final String REDIRECT_LIST = "redirect:/admin/adminblog";

    @Autowired
    private AdminBlogService adminBlogService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private TagService tagService;



    @GetMapping("/adminblog")
    public String Blogs(@PageableDefault(size = 4,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                        BlogQuery blog, Model model){
        model.addAttribute("types",typeService.listType());
        model.addAttribute("page",adminBlogService.listBlog(pageable,blog));
        return "admin/adminblog";
    }

    @PostMapping("/adminblog/search")
    public String search(@PageableDefault(size = 4,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable,
                         BlogQuery blog, Model model){
        model.addAttribute("page",adminBlogService.listBlog(pageable,blog));
        return "admin/adminblog :: blogList";
    }


//    进入文章添加界面
    @GetMapping("/admininput")
    public String admininput(Model model){
        model.addAttribute("types",typeService.listType());
        model.addAttribute("tags",tagService.listTag());
        model.addAttribute("blog",new Blog());
        return INPUT;
    }

    //    进入文章修改界面
    @GetMapping("{id}/adminupdate")
    public String editinput(@PathVariable Long id, Model model){
        model.addAttribute("types",typeService.listType());
        model.addAttribute("tags",tagService.listTag());
        Blog blog = adminBlogService.getBlog(id);
        blog.init();
        model.addAttribute("blog",blog);
        return "admin/adminupdate";
    }

    //文章添加
    @PostMapping("/blogsave")
    public String save(Blog blog, HttpSession session, RedirectAttributes attributes){
        blog.setUser((User) session.getAttribute("user"));
        blog.setType(typeService.getType(blog.getType().getId()));
        blog.setTag(tagService.getTag(blog.getTag().getId()));
//        blog.setTags(tagService.listTag(blog.getTagIds()));

        Blog b;
        if (blog.getId() == null){
            b =  adminBlogService.saveBlog(blog);
        }else {
            b = adminBlogService.updateBlog(blog.getId(),blog);
        }
        if (b == null){
            attributes.addFlashAttribute("message","恭喜，操作错误!");
        }else {
            attributes.addFlashAttribute("message","恭喜，操作成功!");
        }
        return REDIRECT_LIST;
    }

    @GetMapping("{id}/delete")
    public String delete(@PathVariable Long id,RedirectAttributes attributes){
        adminBlogService.deleteBlog(id);
        attributes.addFlashAttribute("message","恭喜，删除成功!");
        return REDIRECT_LIST;
    }
}
